package com.itheima.test04;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
接受图片并保存


给 客户端响应

服务端优化
可以接受多次图片上传

 */
public class ServerTest {

    public static void main(String[] args) throws IOException {
        //创建输出流  用来把 文件保存
        //创建 服务端对象
        ServerSocket ss = new ServerSocket(10001);

        while (true) {
            Socket accept = ss.accept();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy\\meinv.jpg"));
            //建立连接 获取 Socket 对象
            //获取输入流
            InputStream is = accept.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            //边读编写
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {

                bos.write(bytes, 0, len);
            }
            bos.flush();
            // 给客户端写一个响应
            String s = "上传成功";
            OutputStream os = accept.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
            writer.write(s);
       /* writer.flush();
        accept.shutdownOutput();*/
            writer.close();
            //释放资源
            bos.close();
        }


        // ss.close();


    }
}
